<?php
namespace app\admin\model;
use think\Db;
use think\Model;
class Article extends Model{




    //查询栏目id=?下所有文章
    public function column_art($column_id,$page,$limit){
        $start=($page-1)*$limit;

        $info=$this->where("column_id","=",$column_id)->order('id desc')->limit($start,$limit)->field(['content_text','content'],true)->select();//查询本次请求数据

        $num=$this->where("column_id","=",$column_id)->count();//查询数据总条数
        $array=[
            'num'=>$num,
            'info'=>$info
        ];
        return $array;
    }


    //添加文章
    public function addArticle($title,$Article,$thumbnailUrl,$time,$columnId,$ArticleText){
        $columnName=Db::table("column")->where("id","=",$columnId)->value("name");//通过栏目id查询栏目名称


        $resultId=$this->insertGetId([
            'title'=>$title,
            'content'=>$Article,
            'thumbnail'=>$thumbnailUrl,
            'time'=>$time,
            'column_id'=>$columnId,
            'column_name'=>$columnName,
            'content_text'=>$ArticleText
        ]);

        return $resultId;
    }



    /*
     通过文章id输出文章内容
     $id文章id
     */
    public function  selectArticle($id){
        $result=$this->where("id","=",$id)->find();
        if ($result){
            $result=$result->toArray();
        }
        return $result;

    }

    /*
     * 通过当前文章id查询下一篇文章的标题，链接
     * $id 当前文章id
     */
    public function select_Next_Article($id,$columnIds){
        $result=$this->where([
            "column_id"=>['=',$columnIds],//确保是同一栏目下
            "id"=>['>',$id],//查询的id大于当前id
            "static"=>['=','1']//切需要生成静态
        ])->order(['id'=>'ASC'])->limit(1)->find();
        if ($result){
            $result=$result->toArray();
        }else{
            $result=[];
        }

        return $result;

    }

    /*
     * 通过当前文章id查询上一篇文章的标题，链接
     * $id 当前文章id
     */
    public function select_Last_Article($id,$columnIds){
        $result=$this->where([
            "column_id"=>['=',$columnIds],//确保是同一栏目下
            "id"=>['<',$id],//查询的id小于当前id
            "static"=>['=','1']//切需要生成静态
        ])->order(['id'=>'DESC'])->limit(1)->find();
        if ($result){
            $result=$result->toArray();
        }else{
            $result=[];
        }

        return $result;

    }







    /*
     输出所有文章
     */
    public function AllArticle($page,$limit){
        $start=($page-1)*$limit;
        //由于文章内容字段查询比较占用资源所以在修改时在进行逐一查询
        $info=$this->order('id desc')->limit($start,$limit)->field(['content_text','content'],true)->select();//查询本次请求数据
        //$info=$this->order('id desc')->limit($start,$limit)->select();//查询本次请求数据
        $num=$this->count('id');//查询数据总条数
        $array=[
            'num'=>$num,
            'info'=>$info
        ];
        return $array;
    }


    /*
     删除文章
    $ids 要删除的文章id数组
     */
    public function  deleteArticle(array $ids){

        return Article::destroy($ids);

    }

    //修改文章
    public function updateArt($id,$title,$Article,$thumbnailUrl,$time,$columnId,$ArticleText){
        $columnName=Db::table("column")->where("id","=",$columnId)->value("name");//通过栏目id查询栏目名称
        $result =$this->where("id","=",$id)->update([
            'title'=>$title,
            'content'=>$Article,
            'thumbnail'=>$thumbnailUrl,
            'time'=>$time,
            'column_id'=>$columnId,
            'column_name'=>$columnName,
            'content_text'=>$ArticleText
        ]);

        return $result;

    }

    //更新文章url地址、设置文章状态
    public function update_ArtUrl_Static($id,$url,$static){

        $this->where("id","=",$id)->update([
            'url'=>$url,
            'static'=>$static,
        ]);

    }



    public function atr_ids($columnId){
        $result=Db::table("article")->where("column_id","=",$columnId)->field(["id","column_id"])->select();
//        $result=$this->where("column_id","=",$columnId)->column("id");
        return $result;

    }





}
